package com.ikangtai.shecare.curve.mpchart;

import android.graphics.Color;
import android.graphics.drawable.Drawable;

import com.github.mikephil.charting.data.LineDataSet;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import java.util.Date;


public class BBTLineUtil {
    /**
     * 把日期秒数转变为X轴上的x值
     * 以X轴中间值为今天为标准，如200为今天，则昨天为199
     */
    public static float getXAxisValue(long daytimeSeconds){
        Date fDate = new Date();
        Date oDate = new Date(daytimeSeconds * 1000);
        int dayInterval = DateUtil.getDayInterval(fDate, oDate);
        AppLog.i("BBTLineChart dayInterval = " + dayInterval);

        return BBTLineChart.XAXIS_TODAY + dayInterval;
    }

    public static boolean isValidXAxisValue(float x){
        if(x >= BBTLineChart.MIN_XAXIS_DAY && x <= BBTLineChart.MAX_XAXIS_DAY)
            return true;
        else
            return false;
    }

    public static long getDayTimeSeconds(float xAxisValue){
        long nowTime = System.currentTimeMillis() / 1000;
        return (long)(nowTime + (xAxisValue - BBTLineChart.XAXIS_TODAY) * 24 * 3600);
    }

    public static void setLineStyle(LineDataSet lineDataSet){
        lineDataSet.setLineWidth(2f);
        lineDataSet.setCircleRadius(5f);
        lineDataSet.setCircleHoleRadius(3f);
        lineDataSet.setDrawCircleHole(true);
        lineDataSet.setDrawValues(false); // 不在圆圈旁边显示Y值, 默认为true
    }

    /**
     * 判断某天处于什么周期
     * @param daySeconds 此日期的秒数
     * @return
     */
    public static String getPeriod(long daySeconds){
        String period = "UNKOWN";

        Date paramDate = new Date(daySeconds * 1000);

        int dayUnitDSOutputsListLen = App.dayUnitDSOutputsList.size();

        for(int i = 0; i < dayUnitDSOutputsListLen; i++){

            //说明未在 dayUnitDSOutputsList范围之内
            if(i == dayUnitDSOutputsListLen - 1) {
                AppLog.i("BBTLineChart unknown period!");
                break;
            }

            DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(i);
            long dayUnitDSOutputTime = dayUnitDSOutput.date;
            Date thisDate = new Date(dayUnitDSOutputTime * 1000);

            DayUnitDSOutput nextDayUnitDSOutput = App.dayUnitDSOutputsList.get(i + 1);
            long NextDayUnitDSOutputTime = nextDayUnitDSOutput.date;
            Date nextDate = new Date(NextDayUnitDSOutputTime * 1000);

            if(DateUtil.getDayInterval(thisDate, paramDate) == 0){
                period = dayUnitDSOutput.getPeriod(App.status);
            }else if(DateUtil.getDayInterval(nextDate, paramDate) == 0){
                period = nextDayUnitDSOutput.getPeriod(App.status);
            }
        }

        return period;
    }


    /**********************
     * 填充颜色相关
     **********************/

    public static float getFullfilledYValue(float x){
        float fullfilledYValue;

        float validPreviousX = getValidPreviousX(x);
        float validNextX = getValidNextX(x);
        AppLog.i("BBTLineChart x has no y value, need filled! x = " + x + ", validPreviousX = " + validPreviousX + ", validNextX = " + validNextX);

        float validPreviousY = BBTLineChart.bbtLineChartxValue2yValue.get(validPreviousX);
        float valideNextY = BBTLineChart.bbtLineChartxValue2yValue.get(validNextX);

        int totalX = (int) (validNextX - validPreviousX);

        if(validPreviousY <= valideNextY) {
            float totalY = valideNextY - validPreviousY;
            int deltaX = (int) (x - validPreviousX);

            fullfilledYValue = validPreviousY + (deltaX * totalY / totalX);
        }else {
            float totalY = validPreviousY - valideNextY;
            int deltaX = (int) (validNextX - x);

            fullfilledYValue = valideNextY + (deltaX * totalY / totalX);
        }

        return fullfilledYValue;
    }

    private static float getValidPreviousX(float x){
        do{
            x = x - 1;
            if(BBTLineChart.bbtLineChartxValue2yValue.get(x) != null)
                return x;
        }while(true);
    }

    private static float getValidNextX(float x){
        do{
            x = x + 1;
            if(BBTLineChart.bbtLineChartxValue2yValue.get(x) != null)
                return x;
        }while(true);
    }

    public static float getDayoutputFirstX(){
        return getXAxisValue(App.dayUnitDSOutputsList.get(0).date);
    }

    public static float getDayoutputFinalX(){
        return getXAxisValue(App.dayUnitDSOutputsList.get(App.dayUnitDSOutputsList.size() - 1).date);
    }

    public static void setFullFillLineStyle(LineDataSet lineDataSet, Drawable drawable){
        lineDataSet.setLineWidth(0f);
        lineDataSet.setDrawValues(false); // 不在圆圈旁边显示Y值, 默认为true
        lineDataSet.setColor(Color.argb(0, 0, 0, 0));
        lineDataSet.setCircleColor(Color.argb(0, 0, 0, 0));
        lineDataSet.setDrawHorizontalHighlightIndicator(false);
        lineDataSet.setDrawVerticalHighlightIndicator(false);
        lineDataSet.setDrawFilled(true);

        lineDataSet.setFillDrawable(drawable);
    }
}
